<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="卡bin" prop="bin">
        <el-input
          size="mini"
          v-model="queryParams.bin"
          placeholder="请输入卡bin"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡组织" prop="network">
        <el-input
          size="mini"
          v-model="queryParams.network"
          placeholder="请输入卡组织"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支持币种" prop="currencies">
        <el-input
          size="mini"
          v-model="queryParams.currencies"
          placeholder="请输入支持币种"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-select v-model="queryParams.scene" placeholder="请选择状态" clearable size="mini">
          <el-option key="1" label="上架" value="1"/>
          <el-option key="0" label="下架" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-input placeholder="请输入卡bin号" size="mini" v-model="cardBin" class="input-with-select">
          <el-button
            slot="append"
            type="primary"
            size="mini"
            @click="handleMatch"
            v-hasPermi="['system:cardBins:match']"
          >匹配卡种
          </el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" align="center" min-width="5%"/>
      <el-table-column label="卡BIN" align="center" prop="bin"/>
      <el-table-column label="类型" align="center" prop="type"/>
      <el-table-column label="封面图" align="center" key="cover">
        <template slot-scope="scope">
          <image-preview class="list-img" :src="scope.row.cover"/>
        </template>
      </el-table-column>
      <el-table-column label="卡组织" align="center" prop="network"/>
      <el-table-column label="支持币种" align="center" prop="currencies"/>
      <el-table-column label="状态" align="center" prop="status" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleShowStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="getInfo(scope.row)"
            v-hasPermi="['system:cardBins:query']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cardBins:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cardBins:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="封面图:" prop="cover">
          <ImageUpload v-model="form.cover" :limit="1" :file-size="1"/>
        </el-form-item>
        <el-form-item label="卡介绍:">
          <el-table border :data="form.introduce" size="mini">
            <el-table-column prop='icon' label="图标" width="200" align="center">
              <template slot-scope="child_scope">
                <SingleImageUpload :isShowTip="true" v-model="child_scope.row.icon"/>
              </template>
            </el-table-column>
            <el-table-column prop='content' label="介绍标题(中文)" width="350" align="center">
              <template slot-scope="child_scope">
                <el-input v-model.trim="child_scope.row.zh_title" placeholder="介绍标题(中文)" clearable></el-input>
              </template>
            </el-table-column>
            <el-table-column prop='content' label="介绍内容(中文)" width="350" align="center">
              <template slot-scope="child_scope">
                <el-input type="textarea" rows="3" v-model.trim="child_scope.row.zh_content" placeholder="介绍内容(中文)"
                          clearable></el-input>
              </template>
            </el-table-column>
            <el-table-column prop='content' label="介绍标题(英文)" width="350" align="center">
              <template slot-scope="child_scope">
                <el-input v-model.trim="child_scope.row.en_title" placeholder="介绍标题(英文)" clearable></el-input>
              </template>
            </el-table-column>
            <el-table-column prop='content' label="介绍内容(英文)" width="350" align="center">
              <template slot-scope="child_scope">
                <el-input type="textarea" rows="3" v-model.trim="child_scope.row.en_content" placeholder="介绍内容(英文)"
                          clearable></el-input>
              </template>
            </el-table-column>
            <el-table-column prop='content' label="介绍标题(日文)" width="350" align="center">
              <template slot-scope="child_scope">
                <el-input v-model.trim="child_scope.row.ja_title" placeholder="介绍标题(日文)" clearable></el-input>
              </template>
            </el-table-column>
            <el-table-column prop='content' label="介绍内容(日文)" width="350" align="center">
              <template slot-scope="child_scope">
                <el-input type="textarea" rows="3" v-model.trim="child_scope.row.ja_content" placeholder="介绍内容(日文)"
                          clearable></el-input>
              </template>
            </el-table-column>
            <el-table-column width="100" label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  @click="deleteItem(scope.$index, scope.row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button type="primary" style="float: right" size="mini" @click="addItem">添加介绍</el-button>
        </el-form-item>
        <el-form-item label="上架状态:" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">上架</el-radio>
            <el-radio label="1">下架</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="卡种详情" :visible.sync="openDetail" width="80%" append-to-body>
      <el-form ref="form" :model="detail" label-width="150px" style="width: 100%">
        <el-row :gutter="24">
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-form-item label="封面图: ">
                <image-preview class="detail-img" :src="detail.cover"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="卡BIN: ">
                {{ detail.bin }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="类型: ">
                {{ detail.type }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="知识产权: ">
                {{ detail.ipr }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="支持币种: ">
                {{ detail.currencies }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="发卡商: ">
                {{ detail.network }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="是否支持AVS校验: ">
                {{ detail.avs === 1 ? '是' : '否' }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="是否支持3DS验证: ">
                {{ detail.threeDs === 1 ? '是' : '否' }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="每天限购数量: ">
                {{ detail.purchaseLimitDay }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="单次限购数量: ">
                {{ detail.purchaseLimitSingle }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="限期总的购买限制: ">
                {{ detail.purchaseLimitLifetime }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="创建时间: ">
                {{ parseTime(detail.createTime) }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="更新时间: ">
                {{ parseTime(detail.updateTime) }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-form-item label="卡介绍: ">
                <el-table border :data="detail.introduce" size="mini">
                  <el-table-column prop='icon' label="图标" width="200" align="center">
                    <template slot-scope="child_scope">
                      <image-preview class="detail-img" :src="child_scope.row.icon"/>
                    </template>
                  </el-table-column>
                  <el-table-column prop='content' label="介绍标题（中文）" width="350" align="center">
                    <template slot-scope="child_scope">
                      {{ child_scope.row.zh_title }}
                    </template>
                  </el-table-column>
                  <el-table-column prop='content' label="介绍内容（中文）" width="350" align="center">
                    <template slot-scope="child_scope">
                      {{ child_scope.row.zh_content }}
                    </template>
                  </el-table-column>
                  <el-table-column prop='content' label="介绍标题（英文）" width="350" align="center">
                    <template slot-scope="child_scope">
                      {{ child_scope.row.en_title }}
                    </template>
                  </el-table-column>
                  <el-table-column prop='content' label="介绍内容（英文）" width="350" align="center">
                    <template slot-scope="child_scope">
                      {{ child_scope.row.en_content }}
                    </template>
                  </el-table-column>
                  <el-table-column prop='content' label="介绍标题（日文）" width="350" align="center">
                    <template slot-scope="child_scope">
                      {{ child_scope.row.ja_title }}
                    </template>
                  </el-table-column>
                  <el-table-column prop='content' label="介绍内容（日文）" width="350" align="center">
                    <template slot-scope="child_scope">
                      {{ child_scope.row.ja_content }}
                    </template>
                  </el-table-column>
                </el-table>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="状态: ">
                {{ detail.status === '0' ? '上架' : '下架' }}
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {match, pageList, changeStatus, cardDetail, remove, edit} from '@/api/triones/cardBins'

export default {
  name: "index",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bin: undefined,
        network: undefined,
        currencies: undefined,
        status: undefined,
      },
      total: 0,
      dataList: [],
      title: '',
      open: false,
      form: {},
      rules: {
        cover: [
          {required: true, message: "封面不能为空", trigger: "blur"}
        ]
      },
      cardBin: undefined,
      openDetail: false,
      detail: {}
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 上下架 */
    handleShowStatusChange(row) {
      let text = row.status === '0' ? "上架" : "下架";
      this.$modal.confirm('确认要"' + text + '""' + '吗？').then(function () {
        return changeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === 0 ? 0 : 1;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        cover: undefined,
        status: undefined,
        introduce: []
      };
      this.resetForm("form");
    },
    handleMatch() {
      if (!this.cardBin) {
        return this.$message.error("卡BIN号不能为空！")
      }
      this.loading = true;
      const data = {
        bin: this.cardBin
      }
      match(data).then(response => {
        this.$message.success("匹配成功！")
        this.cardBin = undefined;
        this.getList();
      })
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id
      cardDetail(id).then(response => {
        this.form = response.data;
        this.form.introduce = JSON.parse(response.data.introduce) || [];
        this.title = "修改卡BIN";
        this.open = true;
      });
    },
    getInfo(row) {
      const id = row.id
      cardDetail(id).then(response => {
        this.detail = response.data;
        this.detail.introduce = JSON.parse(response.data.introduce) || [];
        this.openDetail = true;
      });
    },
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除数据项？').then(function () {
        return remove(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const hasEmptyContent = this.form.introduce.some(item => {
            return !item.zh_content.trim() || !item.en_content.trim() || !item.ja_content.trim();
          });
          if (hasEmptyContent) {
            this.$modal.msgError("卡介绍的三种语言内容均不能为空！");
            return;
          }
          if (this.form.id !== undefined) {
            this.$modal.confirm('是否确认该操作？').then(() => {
              this.form.introduce = JSON.stringify(this.form.introduce);
              edit(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      })
    },
    addItem() {
      var _this = this
      let list = {
        icon: '',
        zh_title: '',
        zh_content: '',
        en_title: '',
        en_content: '',
        ja_title: '',
        ja_content: '',
      }
      _this.form.introduce.push(list)
    },
    deleteItem(index, row) {
      var _this = this
      _this.form.introduce.splice(index, 1)
    }
  }
}
</script>
<style scoped lang="scss">

</style>
